from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

class LayersDialog(QWidget): 
    def __init__(self, editor, parent = None):
        QDialog.__init__(self, parent)
        self.resize(150, 200)
        self.editor = editor
        self.editor.layers_dialog = self

        self.initialize_dialog()
        self.initialize_list()

    def initialize_dialog(self):
        self.vbox = QVBoxLayout()
        self.list = QListWidget()
        self.connect(self.list, SIGNAL("currentRowChanged(int)"), self.row_changed)
        self.buttonhboxcontainer = QWidget()
        self.buttonhbox = QHBoxLayout(self.buttonhboxcontainer)
        self.buttons = {}

        self.buttons["add_layer"] = QPushButton("+")
        self.buttons["del_layer"] = QPushButton("-")
        self.buttons["move_layer_up"] = QPushButton("Up")
        self.buttons["move_layer_down"] = QPushButton("Down")
        self.buttons["merge_layer_down"] = QPushButton("Merge")
        self.buttons["copy_to_all_frames"] = QPushButton("To All")

        for key,button in self.buttons.items():
            self.connect(button, SIGNAL("clicked()"), getattr(self,key))
            self.buttonhbox.addWidget(button)
            self.buttonhbox.setAlignment(button, Qt.AlignLeft)

        self.buttonhbox.insertStretch(2)
        self.buttonhbox.setContentsMargins(0,0,0,0)

        self.main_hbox = QHBoxLayout()
        self.group = QGroupBox("Layers")
        self.vbox.addWidget(self.list)
        self.vbox.addWidget(self.buttonhboxcontainer)
        self.group.setLayout(self.vbox)
        self.main_hbox.addWidget(self.group)

        self.setLayout(self.main_hbox)
        
        self.connect(self.editor, SIGNAL("painted()"), self.initialize_list)

    def move_layer_up(self):
        index = self.editor.get_selected_frame().move_layer_up(self.list.currentRow())
        if index != None:
            self.initialize_list()
            self.editor.update()
            self.list.setCurrentRow(index)

    def move_layer_down(self):
        index = self.editor.get_selected_frame().move_layer_down(self.list.currentRow())
        if index != None:
            self.initialize_list()
            self.editor.update()
            self.list.setCurrentRow(index)

    def merge_layer_down(self):
        index = self.editor.get_selected_frame().merge_layer_down(self.list.currentRow())
        if index != None:
            self.initialize_list()
            self.editor.update()
            self.list.setCurrentRow(index)

    def copy_to_all_frames(self):
        sprite = self.editor.sprite
        frame = self.editor.get_selected_frame()
        layer_index = self.list.currentRow()

        sprite.copy_layer_to_all_frames(frame, layer_index)
        
    def row_changed(self, ev):
        if self.list.currentRow() >= 0:
	        self.editor.set_selected_layer(self.list.currentRow())
        
    def add_layer(self):
        for frame in self.editor.sprite.frames:
	        index = frame.add_empty_layer()
        self.initialize_list()
        self.editor.update()
        self.list.setCurrentRow(index)
        
    def del_layer(self):
        for frame in self.editor.sprite.frames:
	        if len(frame.layers) > 1:
		        frame.delete_layer(frame.layers[self.list.currentRow()])
		        self.list.setCurrentRow(len(frame.layers))
        self.initialize_list()
        self.editor.update()
        
    def update_list(self):
        for i in range(self.list.count()):
            item = self.list.item(i)
            for frame in self.editor.sprite.frames:
	            layer = frame.layers[i]
	            icon = QIcon()
	            icon.addPixmap(layer)
	            item.setIcon(icon)
        
    def initialize_list(self):
        self.list.clear()
        #for sprite in self.editor.sprites:
        for layer in self.editor.get_selected_frame().layers:
            item = QListWidgetItem("Layer " + str(self.editor.get_selected_frame().layers.index(layer) + 1))
            icon = QIcon()
            icon.addPixmap(layer)
            item.setIcon(icon)
            item.setSizeHint(QSize(25,25))
            self.list.addItem(item)
        self.list.setCurrentRow(self.editor.selected_layer, QItemSelectionModel.Select)
